package com.lcicl.reggie.service.impl;
/*
  Date: 2023/08/10
  Author: Luochao
  Description:
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcicl.reggie.entity.Category;
import com.lcicl.reggie.entity.Dish;
import com.lcicl.reggie.entity.Setmeal;
import com.lcicl.reggie.mapper.CategoryMapper;
import com.lcicl.reggie.service.CategoryService;
import com.lcicl.reggie.service.DishService;
import com.lcicl.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        //判断分类是否有指向
        Category category = this.getById(id);
        Integer type = category.getType();
        if (type == 1) {
            //删除菜品
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dish::getCategoryId, id);
            dishService.remove(queryWrapper);
        } else if (type == 2) {
            //删除套餐
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getCategoryId, id);
            List<Setmeal> list = setmealService.list(queryWrapper); //该套餐分类下的所有套餐
            List<Long> collect = list.stream().map(setmeal -> {
                return setmeal.getId();
            }).collect(Collectors.toList());
            setmealService.deleteSetmeals((ArrayList<Long>) collect);
        }
        boolean b = this.removeById(id);

        return b;
    }
}
